﻿#region Logger Explanation
/**
	 *			Class : Logger.cs
	 *			Obj : Trace Information, Warning, Error in a HTML page
	 *			Author : http://www.david-amador.com 
	 *			Modify by : Lorentz Benjamin
	 *			Add by : Lorentz Benjamin
	 *			Use : 
	 *				Logger.Initialize();
	 *				var l = new Logger("Main");
	 *				l.Info("You can use logger now");
	 *				l.Debug("To trace debug data");
	 *				l.Critical("Or critical event");
	 * 
	 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FlanProjectData.Helpers
{
	public enum Log_Type
	{
		DEBUG,		// Testing message
		INFO,		// Information message for an action
		WARNING,	// Unusual event
		ERROR,		// Fonctionnal error
		CRITICAL,	// Dramatic error
	}

	public class Logger
	{
		public static bool _active;						// In case you want to deactivate the logger
		private string forClass;						// Define the class to trace
		private Log_Type traceLevel = Log_Type.DEBUG;	// Define the level of tracing. 
		static private string fontName = "consolas";			// Define the font on the html page
		static private string fileName = @"..\..\..\..\..\log.html";			// Define where log are written
		private string previousMessage = "";

		public bool autoRefresh { get; set; }

		#region Constructors

		/// <summary>
		/// Constructor for the Logger
		/// </summary>
		/// <param name="className">Name classe who use it</param>
		public Logger(string className)
		{
			this.forClass = className;
		}

		public Logger(string className, Log_Type traceLevel)
		{
			this.forClass = className;
			this.traceLevel = traceLevel;
		}

		#endregion

		static public void Initialize()
		{
			_active = true;
			//#if WINDOWS
            try
            {
                StreamWriter textOut = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write));
                // Refresh the page in the web-browser
				//textOut.WriteLine("<meta http-equiv=\"refresh\" content=\"10;" + fileName + "\" />");
                textOut.WriteLine("<h3>Log File</h3>");
                textOut.WriteLine("");
                textOut.WriteLine("<span style=\"font-family: '" + fontName + "\'; color: black;\">");
                textOut.WriteLine("Log started at " + System.DateTime.Now.ToLongTimeString() + "</span><hr />");
                textOut.Close();
                //#endif
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : " + e.Message);
            }
		}

		public bool Active
		{
			get { return _active; }
			set { _active = value; }
		}

		#region Write to HTML

		private void log(Log_Type type, string text)
		{
			//#if WINDOWS
			if (!_active) return;
			string begin = "";
			if(!text.Equals(previousMessage))
			{
				previousMessage = text;
				if ((int)type >= (int)this.traceLevel)
				{
					switch (type)
					{
						case Log_Type.DEBUG:
							begin = "<span style=\"font-family: '" + fontName + "\'; color: black;\">[DEBG] ";
							break;
						case Log_Type.INFO:
							begin = "<span style=\"font-family: '" + fontName + "\'; color: green;\">[INFO] ";
							break;
						case Log_Type.WARNING:
							begin = "<span style=\"font-family: '" + fontName + "\'; color: orange;\">[WARN] ";
							break;
						case Log_Type.ERROR:
							begin = "<span style=\"font-family: '" + fontName + "\'; color:red; font-weight:bold; \">[ERRO] ";
							break;
						case Log_Type.CRITICAL:
							begin = "<span style=\"font-family: '" + fontName + "\'; color: black;blackborder:2px solid black;background-color: red;\">[CRIT] ";
							break;
					}
					text = begin + System.DateTime.Now.ToLongTimeString() + " : [" + this.forClass + "] " + text + "</span><br/>";
					Output(text);
					//#endif
				}
			}
		}

		public void Error(string trace)
		{
			log(Log_Type.ERROR, trace);
		}
		public void Warn(string trace)
		{
			log(Log_Type.WARNING, trace);
		}
		public void Info(string trace)
		{
			log(Log_Type.INFO, trace);
		}
		public void Debug(string trace)
		{
			log(Log_Type.DEBUG, trace);
		}
		public void Critical(string trace)
		{
			log(Log_Type.CRITICAL, trace);
		}

		static private void Output(string text)
		{
			//#if WINDOWS
			try
			{
				StreamWriter textOut = new StreamWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write));
				textOut.WriteLine(text);
				textOut.Close();
			}
			catch (System.Exception e)
			{
				string error = e.Message;
			}
		}

		#endregion
	}
			//#endif
}
